package com.antoco.operation.bigscreen.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DateUtils {

  /**
   * 收集起始时间到结束时间之间所有的日期并以集合的方式返回
   * @param startTime
   * @param endTime
   * @return
   */
  public static List<String> CollectLocalDates(String startTime, String endTime) {
    return collectLocalDates(LocalDate.parse(startTime), LocalDate.parse(endTime));
  }

  /**
   * 收集起始时间到结束时间之间所有的日期并以集合的方式返回
   * @param startTime
   * @param endTime
   * @return
   */
  public static List<String> collectLocalDates(LocalDate startTime, LocalDate endTime) {
    return Stream.iterate(startTime, LocalDate -> LocalDate.plusDays(1))
            .limit(ChronoUnit.DAYS.between(startTime, endTime) + 1)
            .map(LocalDate::toString)
            .collect(Collectors.toList());
  }

  /**
   * 收集起始年份到结束年份之间所有的年份并以集合的方式返回
   * @param startYear
   * @param endYear
   * @return
   */
  public static List<Integer> CollectLocalYears(String startYear, String endYear) {
    if (StringUtil.isBlank(startYear) || StringUtil.isBlank(endYear)) {
        return new ArrayList<>();
    }
    return collectLocalYears(LocalDate.parse(startYear + "-01-01"), LocalDate.parse(endYear + "-01-01"));
  }

  public static List<Integer> collectLocalYears(LocalDate startTime, LocalDate endTime) {
    return Stream.iterate(startTime, LocalDate -> LocalDate.plusYears(1))
            .limit(ChronoUnit.YEARS.between(startTime, endTime) + 1)
            .map(LocalDate::getYear)
            .collect(Collectors.toList());
  }

  /**
   * 格式化出生日期并得到年龄
   * @param year
   * @param month
   * @param day
   */
  public static Integer getFormatAge(Integer year, Integer month, Integer day) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    if (null == year || null == month || null == day
            || 0 == year || 0 == month || 0 == day) {
        return null;
    }
    try {
        Date birthDay = sdf.parse(year + "-" + month + "-" + day);
        return getAge(birthDay);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
  }

  /**
   * 由出生日期获得年龄
   * @param birthDay
   * @return
   */
  public static int getAge(Date birthDay) throws Exception {
    Calendar cal = Calendar.getInstance();
    if (cal.before(birthDay)) {
        throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
    }
    int yearNow = cal.get(Calendar.YEAR);
    int monthNow = cal.get(Calendar.MONTH);
    int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
    cal.setTime(birthDay);

    int yearBirth = cal.get(Calendar.YEAR);
    int monthBirth = cal.get(Calendar.MONTH);
    int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

    int age = yearNow - yearBirth;

    if (monthNow <= monthBirth) {
        if (monthNow == monthBirth) {
            if (dayOfMonthNow < dayOfMonthBirth) {
                age--;
            }
        }else{
            age--;
        }
    }
    return age;
  }


}
